package xiaoqiang

import (
	"encoding/json"
	"fmt"
	"parking-lot-go-push/model/cars"
	"parking-lot-go-push/model/order"
	"parking-lot-go-push/mongodb"
)

type EntryStruct struct {
	mongodb.CarsEntryMongoDbStruct
}

// 小强进场推送
func (entryData EntryStruct) Entry() {

	xiaoQiangPushLogger.Info(" ")
	xiaoQiangPushLogger.Info("---入场推送---")
	jsonData, _ := json.Marshal(entryData)
	xiaoQiangPushLogger.Info(fmt.Sprintf("参数:%s", string(jsonData)))

	//错误信息
	var err error

	//查询订单
	var orderStruct order.OrderStruct
	if _, err = orderStruct.FetchOrder(entryData.OrderNo, 0, 1); err != nil {
		xiaoQiangPushLogger.Error(fmt.Sprintf("订单查询失败:%s", err.Error()))
		return
	}

	//查询小强订单
	var orderXiaoQiangStruct order.OrderXiaoqiangStruct
	orderXiaoQiangData, err := orderXiaoQiangStruct.FetchOrder(entryData.OrderNo, 0)
	if err != nil {
		xiaoQiangPushLogger.Error(fmt.Sprintf("订单查询失败:%s", err.Error()))
		return
	}

	//超停状态
	abnormal, err := orderStruct.OverStop(entryData.ParkCode, 1, entryData.OrderNo)
	if err != nil {
		xiaoQiangPushLogger.Error(err.Error())
		return
	}

	//通知小强车辆入场
	xiaoQiangEntryPushData := map[string]interface{}{
		"serviceType": 2001,
		"orderType":   1,
		"orderId":     orderXiaoQiangData.XqOrderNo,
		"parkId":      entryData.ParkCode,
		"plateNumber": entryData.CarNumber,
		"startTime":   entryData.InTime,
	}

	//加密信息计算
	xqSigner := NewSigner()
	encryptedData, signature, err := xqSigner.EncryptAndSign(xiaoQiangEntryPushData)
	if err != nil {
		xiaoQiangPushLogger.Error(fmt.Sprintf("加密失败:%s", err.Error()))
		return
	}

	var carsEntryStruct cars.CarsEntryStruct
	//推送信息
	if err, _ := xqSigner.CurlPostJson(
		"inOutPark",
		map[string]interface{}{
			"data": encryptedData,
			"sign": signature,
		}); err != nil {
		xiaoQiangPushLogger.Error(fmt.Sprintf("推送失败:%s", err.Error()))

		//失败状态更新
		if err := carsEntryStruct.PushError(entryData.OrderNo, entryData.PushErrNum); err != nil {
			xiaoQiangPushLogger.Error(err.Error())
		}
	} else {
		//成功状态更新
		if err := carsEntryStruct.PushSuccess(entryData.OrderNo, entryData.InTime, entryData.ParkCode, entryData.OrderType, abnormal); err != nil {
			xiaoQiangPushLogger.Error(err.Error())
		}
	}

}
